package com.geosetting;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class GeoSetting extends Activity {

	Button getLocationWiFi, addLocation, getLocation, getCurrentLocation, onMap;
	TextView textbox, showlocation;
	EditText currentLocationText;
	ToggleButton wifi, serviceState;
	RadioButton gpsRb, wifiRb;
	ListView listview;
	CheckBox bluetoothCb, wifiCb, callCb;
	Toast abc;
	String tex;
	ProgressDialog dialog;

	double latitute;
	ArrayList<Locations> locationList = new ArrayList<Locations>();
	double longitude;
	boolean clicked = true;
	public LocationManager myLocationManager;
	public LocationListener myLocationListener;
	private Location currentLocation;

	int mMinute;

	/** Called when the activity is first */

	public void onReceive(Context context, Intent intent) {
		String intentAction = intent.getAction();
		if (intentAction.equals("android.intent.action.PROXIMITY_ALERT")) {
			showToast("intent working");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		textbox = (TextView) findViewById(R.id.TextView01);
		wifi = (ToggleButton) findViewById(R.id.wifiButton);
		serviceState = (ToggleButton) findViewById(R.id.StartService);
		getLocationWiFi = (Button) findViewById(R.id.getLocationWiFi);
		getLocation = (Button) findViewById(R.id.getLocation);
		currentLocationText = (EditText) findViewById(R.id.currentLocationText);
		showlocation = (TextView) findViewById(R.id.showLocation);
		getCurrentLocation = (Button) findViewById(R.id.getCurrentLocation);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
		addLocation = (Button) findViewById(R.id.addLocation);
		bluetoothCb = (CheckBox) findViewById(R.id.bluetoothTick);
		wifiCb = (CheckBox) findViewById(R.id.wifiTick);
		callCb = (CheckBox) findViewById(R.id.callTick);
		onMap = (Button) findViewById(R.id.onMap);
		gpsRb = (RadioButton) findViewById(R.id.RadioButton01);
		wifiRb = (RadioButton) findViewById(R.id.RadioButton02);
		
		onMap.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				  
				  String uri = "geo:" + latitute + "," +
				 longitude; Intent intent = new
				 Intent(Intent.ACTION_VIEW, Uri.parse(uri) );
				 startActivity(intent);
				 
				
			}
		});
		gpsRb.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				
				if (wifiRb.isChecked()) { wifiRb.setChecked(false);
			
				} 
			}
		});
		wifiRb.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				
				if (gpsRb.isChecked()) { gpsRb.setChecked(false);
					
				} 
			}
		});
		serviceState.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				final LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

			}
		});

		wifi.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

				if (wifi.isChecked()) {
					wifiManager.setWifiEnabled(true);
				} else {
					wifiManager.setWifiEnabled(false);
				}
			}
		});

		addLocation.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				final LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
				List<String> providers = lm.getProviders(true);

				/*
				 * Loop over the array backwards, and if you get an accurate
				 * location, then break out the loop
				 */
				Location l = null;

				for (int i = providers.size() - 1; i >= 0; i--) {
					l = lm.getLastKnownLocation(providers.get(i));
					if (l != null)
						break;
				}
				if (l != null) {
					l.setAccuracy(Criteria.ACCURACY_COARSE);
					latitute = l.getLatitude();
					longitude = l.getLongitude();

					Locations p1 = new Locations(latitute, longitude,
							currentLocationText.getText().toString());
					locationList.add(p1);
					/*showlocation.setText("Latitute" + latitute + "Longtitute\t"
							+ longitude);*/
				}
				tex = "";
				for (int i = 0; i < locationList.size(); i++) {

					tex += "latitute: " + locationList.get(i).getLatitue()
							+ "longitude :"
							+ locationList.get(i).getLongitude() + "Location\t"
							+ locationList.get(i).getAddress() + "\n";
				}
				showToast(tex);
			}
		});

		getCurrentLocation.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				final LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

				final Calendar c = Calendar.getInstance();

				mMinute = c.get(Calendar.SECOND);
				if(gpsRb.isChecked())
				{	dialog = ProgressDialog.show(GeoSetting.this,
						"GPS Trangulation", "Loading  GPS via A-GPS", true);}
				else
					
				dialog = ProgressDialog.show(GeoSetting.this,
						"Wifi Trangulation", "Loading  GPS via wifi", true);

				dialog.setCancelable(true);
				Handler handler = new Handler();

				/*
				 * handler.postDelayed(new Runnable() { public void run() {
				 * 
				 * {
				 * 
				 * dialog.dismiss();
				 * 
				 * 
				 * } } }, 1000);
				 */
				
if(wifiRb.isChecked()){
	lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
			0, new LocationListener() {

				public void onStatusChanged(String provider,
						int status, Bundle extras) {
				}

				public void onProviderEnabled(String provider) {
				}

				public void onProviderDisabled(String provider) {
				
					dialog.dismiss();
					showToast("Please enable gps");
				}

				public void onLocationChanged(Location location) {
					clicked = false;
					dialog.dismiss();
					handleLocationChanged(location);

					location.setAccuracy(Criteria.ACCURACY_COARSE);
				showToast(String
							.valueOf(currentLocation.getLatitude())
							+ String.valueOf(currentLocation
									.getLongitude()));

				
					
				}
			});
}
else
{
	lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
			0, new LocationListener() {

				public void onStatusChanged(String provider,
						int status, Bundle extras) {
				}

				public void onProviderEnabled(String provider) {
				}

				public void onProviderDisabled(String provider) {
					dialog.dismiss();
					showToast("Please enable gps");
				}

				public void onLocationChanged(Location location) {
					clicked = false;
					dialog.dismiss();
					handleLocationChanged(location);

					location.setAccuracy(Criteria.ACCURACY_COARSE);
				showToast(String
							.valueOf(currentLocation.getLatitude())
							+ String.valueOf(currentLocation
									.getLongitude()));

					/*
					 * 
					 * String uri = "geo:" + latitute + "," +
					 * longitude; Intent intent = new
					 * Intent(Intent.ACTION_VIEW, Uri.parse(uri) );
					 * startActivity(intent);
					 */
				}
			});
}
				
			}
		});

		getLocation.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				final LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

				lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
						new LocationListener() {

							public void onStatusChanged(String provider,
									int status, Bundle extras) {
							}

							public void onProviderEnabled(String provider) {
							}

							public void onProviderDisabled(String provider) {
								showlocation.setText("Please  enable gps");
							}

							public void onLocationChanged(Location location) {
								handleLocationChanged(location);
								showlocation.setText(String.valueOf(location
										.getLatitude())
										+ String.valueOf(location
												.getLongitude()));

								lm.removeUpdates(this);
								/*
								 * 
								 * String uri = "geo:" + latitute + "," +
								 * longitude; Intent intent = new
								 * Intent(Intent.ACTION_VIEW, Uri.parse(uri) );
								 * startActivity(intent);
								 */
							}
						});
			}
		}// end of listerner

		);
		// end of class

	}

	private void handleLocationChanged(Location loc) {
		// Save the latest location
		this.currentLocation = loc;
		// Update the latitude & longitude TextViews

	}

	@Override
	public void onPause() {
		Toast.makeText(this, "Put your message here", Toast.LENGTH_SHORT)
				.show();
		super.onPause();

	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 0, "Quit");

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1: {
			this.finish();
		}
			return true;

		}
		return false;
	}

	public void showToast(CharSequence message) {
		int duration = Toast.LENGTH_SHORT;

		Toast toast = Toast
				.makeText(getApplicationContext(), message, duration);
		toast.show();
	}

}